package com.episode.duetapi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.episode.duetapi.mapper.MenuMapper;
import com.episode.duetapi.model.Menu;
import com.episode.duetapi.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService{
	
	@Autowired
	private MenuMapper menuMapper;

	@Override
	public List<Menu> getMenuList(String keyWord) {
		List<Menu> menuList = menuMapper.getMenuList(keyWord);
		List<Menu> resultList = new ArrayList<Menu>();
		Map<String,List<Menu>> map = new HashMap<String,List<Menu>>();
		if(null != menuList && menuList.size()>0) {
			for(Menu menu:menuList) {
				if("0".equals(menu.getParentid())) {
					resultList.add(menu);
				}else {
					List<Menu> temp = map.get(menu.getParentid())== null ?new ArrayList<Menu>():map.get(menu.getParentid());
					temp.add(menu);
					map.put(menu.getParentid(), temp);
				}
			}
			for(Menu mm:resultList) {
				mm.setChildren(map.get(String.valueOf(mm.getId())));
			}
		}
		return resultList;
	}

	@Override
	public void insertMenu(Menu menu) {
		menuMapper.insert(menu);
	}

	@Override
	public void editMenu(Menu menu) {
		
		menuMapper.updateByPrimaryKeySelective(menu);
	}

	@Override
	public List<Menu> getParentMenu() {
		
		return menuMapper.getParentMenu();
	}

	@Override
	public void deleteMenu(Integer id) {
		
		menuMapper.deleteByPrimaryKey(id);
		
	}

}
